package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.JComboBox;

import client.CcrmApp;
import model.Envelope;
import model.CampaignPatern;
import model.Permission;
import view.CampaignPaternGUI;
/**
 * This Class is a Campaign Pattern Controller, which extends AbstractTransfer
 * @author Moran
 *
 */
public class CampaignPaternController extends AbstractTransfer {
	private CampaignPaternGUI TheView;
	private MarketingController prevComt;
	private ArrayList<Integer> ar;
	private ArrayList<String> arr;
	private CampaignPaternController currCon;
	private CampaignPatern CampaignPatern;
	
	/**
	 * Constructor function
	 * @param TheView is CampaignPaternGUI instance
	 * @param tempCon is MarketingController instance which is previous Controller
	 */
	public CampaignPaternController(CampaignPaternGUI TheView,MarketingController tempCon ){
		this.TheView = TheView;
		this.prevComt = tempCon;
		currCon = this;
		TheView.addcancelButtonlesrepor(new cancleBottonListener());
		TheView.addNextBottonActionListener(new NextListener());
		inititemDetails();
	}
	/**
	 * Initialize item combobox
	 */
	public void inititemDetails(){
          ar = new ArrayList<Integer>();
		Envelope ev1 = new Envelope(ar,"get items");
		CcrmApp.clien.setCurrObj(currCon);
		sendToServer(ev1);
	}
	/**
	*Inner class that handles when Button cancel Pressed, implements ActiontListener
	* @author Moran
	*/
	class cancleBottonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			TheView.dispose();
			prevComt.MarkGui.setVisible(true);		
		}
	}
	/**
	*Inner class that handles when Button next Pressed from the first panel, implements ActiontListener
	* @author Moran
	*/
	class NextListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
				CampaignPatern= new CampaignPatern();//save item ID in the CampaignPatern model
				String[] temp = null;
				try{
					if (TheView.getItem().contains(": "))
						temp = TheView.getItem().split(": ");
					int itemid =Integer.parseInt(temp[1]);
					CampaignPatern.setItemid(itemid);
					
					TheView.initPatern2();//Initialize panel 2
					TheView.addNextBotton2ActionListener(new Next2Listener());
				//	TheView.addbackActionListener(new backListener());
					TheView.addcancelButtonlesrepor(new cancleBottonListener());
			        arr = new ArrayList<String>();//Initialize combobox area from DB
			  		Envelope ev1 = new Envelope(ar,"get areas");
			  		CcrmApp.clien.setCurrObj(currCon);
			  		sendToServer(ev1);
			        arr = new ArrayList<String>();//Initialize permission checkbox from DB
			  		Envelope ev2 = new Envelope(ar,"get all permissions");
			  		CcrmApp.clien.setCurrObj(currCon);
			  		sendToServer(ev2);
				}catch(Exception e3){
					System.out.println("no input");
					TheView.warningMas("illigle input error");
				//}
			}	
		}
	}
	/**
	*Inner class that handles when Button next Pressed from the second panel, implements ActiontListener
	* @author Moran
	*/
	class Next2Listener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
				try{
					CampaignPatern.setArea(TheView.getArea());//save parameters in CampaignPatern model
					CampaignPatern.setIntrestLevel(Integer.parseInt(TheView.getIntrest()));
					CampaignPatern.setCostumeRrating(Integer.parseInt(TheView.getRating()));
					String[] temp = null;
					if (TheView.getAge().contains(","))
						temp = TheView.getAge().split(",");
					CampaignPatern.setAgefrom(Integer.parseInt(temp[0]));
					CampaignPatern.setAgeto(Integer.parseInt(temp[1]));
					ArrayList<String> arr=new ArrayList<String>();//save permissions in CampaignPatern model
					arr=TheView.getPermission();
					for (int i=0 ;i<arr.size();i++){
						CampaignPatern.addPermissionArry(new Permission(arr.get(i)));
					}
					TheView.initPatern3();//Initialize panel 3
					TheView.addFinishBottonActionListener(new FinishListener());
				//	TheView.addback2ActionListener(new back2Listener());
					TheView.addcancelButtonlesrepor(new cancleBottonListener());
					
				}catch(Exception e3){
					System.out.println("no input");
					TheView.initPatern2();
					TheView.warningMas("illigle input error");
				//}	
			}
		}
	}
	/**
	*Inner class that handles when Button back Pressed, implements ActiontListener
	* @author Moran
	*/
	class backListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			TheView.initPaternl();
		}
	}
	/**
	*Inner class that handles when Button finish Pressed, implements ActiontListener
	* @author Moran
	*/
	class FinishListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			try{
				if(TheView.getmassagestr().equals("")){
					System.out.println("no input");
					TheView.warningMas("illigle input error");
				}else{
					CampaignPatern.setMessage(TheView.getmassagestr());//save massage
					Envelope ev3 = new Envelope(CampaignPatern,"set CampaignPatern in DB");
			  		CcrmApp.clien.setCurrObj(currCon);//send to server
			  		sendToServer(ev3);
					System.out.println("set CampaignPatern in DB");
				}
			}catch(Exception e4){
				System.out.println("no input");
				TheView.initPatern3();
				TheView.warningMas("illigle input error");
			}
		}
	}
	/**
	*Inner class that handles when Button back2 Pressed, implements ActiontListener
	* @author Moran
	*/
	class back2Listener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			TheView.initPatern2();
		}
	}
	
	/**
	 * handle DB Result
	 * @param Object message
	 */
	 @SuppressWarnings("unchecked")
	 @Override
	public void handleDBResult(Object message) {
		 Envelope ev = (Envelope)message;
		 if(ev.getTask().equals("get items")){//Initialize item combo box
		  if(ev.getMess().equals("no items")){
		  }
		  else{
			for(int i = 0; i < ((ArrayList<Integer>)ev.getObject()).size() ; i++){
				String str = "Item number: " + Integer.toString(((ArrayList<Integer>)ev.getObject()).get(i));
				TheView.getItemCombo().addItem(str);
				}
		    }
		 }
		 if(ev.getTask().equals("get areas")){//Initialize area combo box
			  if(ev.getMess().equals("not found areas")){
			  }
			  else{

				for(int i = 0; i < ((ArrayList<String>)ev.getObject()).size() ; i++){
					String str = ((ArrayList<String>)ev.getObject()).get(i);
					TheView.getareaCombo().addItem(str);
					}
			    }
			 }
		 if(ev.getTask().equals("get all permissions")){//Initialize permissions check box
			 System.out.println("geting all premishion checkbox");
			  if(ev.getMess().equals("not found areas")){
				  TheView.warningMas("not found areas");
			  }
			  else{
				  if(TheView.getck()==null||TheView.getck().isEmpty()){
					  for(int i = 0; i < ((ArrayList<String>)ev.getObject()).size() ; i++){
						  String str = ((ArrayList<String>)ev.getObject()).get(i);
						  System.out.println("add checkbox "+str);
						  TheView.getpremishencheckbox(str);
						  }
				  	}
			  	}
			  } 
		 if(ev.getTask().equals("set CampaignPatern in DB")){//item added or not
			 System.out.println("CampaignPatern in DB back");
			  if(ev.getMess().equals("CampaignPatern added successfully")){
				// TheView.initPaternl();		
				// TheView.warningMas("Campaign Patern added successfully");
				  TheView.dispose();
				  prevComt.getMarkGui().setVisible(true);
				  
			  }
			  else{
				  TheView.initPaternl();
				  TheView.warningMas("Input error");
			  }
		 } 
	 }
}
